const state = {
  canvasList: JSON.parse(localStorage.getItem("canvasList")) || [],
  canvas: JSON.parse(localStorage.getItem("canvas")) || "",
};
const mutations = {
  SET_CANVAS(state, canvas) {
    state.canvasList.push({ ...canvas });
    localStorage.setItem("canvasList", JSON.stringify(state.canvasList));
    mutations.UPDATE_CANVAS(state, canvas);
  },
  REMOVE_CANVAS(state, canvas) {
    const index = state.canvasList.findIndex((item) => item.id === canvas.id);
    if (index !== -1) {
      state.canvasList.splice(index, 1);
      localStorage.setItem("canvasList", JSON.stringify(state.canvasList));
      mutations.UPDATE_CANVAS(state, "");
    }
  },
  UPDATE_CANVAS(state, canvas) {
    state.canvas = canvas;
    localStorage.setItem("canvas", JSON.stringify(canvas));
  },
};
const actions = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
